package com.cndw.kungfu.model.goods;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.vo.EventNotice;

/**
 * 玩家交易对象
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on 2011-9-30
 */
public class PlayerTrade {

	/**
	 * 请求交易的玩家Id
	 */
	private long reqPlayerId;

	/**
	 * 请求交易的玩家名
	 */
	private String reqPlayerName;

	/**
	 * 请求者职业
	 */
	private int reqVocationId;

	/**
	 * 请求交易的玩家等级
	 */
	private int reqPlayerLevel;

	/**
	 * 接受请求的玩家id
	 */
	private long respPlayerId;

	/**
	 * 接收请求的玩家名
	 */
	private String respPlayerName;
	/**
	 * 接收者职业
	 */
	private int respVocationId;
	/**
	 * 接受请求的玩家等级
	 */
	private int respPlayerLevel;

	/**
	 * 1表示请求者确认过了，2表示响应者确认过了,3表示全部确认过了
	 */
	private int status;

	/**
	 * 交易物品集合
	 */
	private Map<Long, List<Integer>> goods = new HashMap<Long, List<Integer>>();

	/**
	 * 银两和金币 {asset,gold}
	 */
	public Map<Long, Integer[]> asset = new HashMap<Long, Integer[]>(2);

	public PlayerTrade(Player reqPlayer, Player respPlayer) {

		this.reqPlayerId = reqPlayer.getPlayerId();
		this.reqPlayerName = reqPlayer.getPlayerName();
		this.reqVocationId = reqPlayer.getVocationId();
		this.reqPlayerLevel = reqPlayer.getLevel();
		this.respPlayerId = respPlayer.getPlayerId();
		this.respPlayerName = respPlayer.getPlayerName();
		this.respVocationId = respPlayer.getVocationId();
		this.respPlayerLevel = respPlayer.getLevel();

		asset.put(reqPlayerId, new Integer[] { 0, 0 });
		asset.put(respPlayerId, new Integer[] { 0, 0 });
		goods.put(reqPlayerId, new ArrayList<Integer>());
		goods.put(respPlayerId, new ArrayList<Integer>());
	}

	public long getReqPlayerId() {
		return reqPlayerId;
	}

	public void setReqPlayerId(long reqPlayerId) {
		this.reqPlayerId = reqPlayerId;
	}

	public String getReqPlayerName() {
		return reqPlayerName;
	}

	public void setReqPlayerName(String reqPlayerName) {
		this.reqPlayerName = reqPlayerName;
	}

	public long getRespPlayerId() {
		return respPlayerId;
	}

	public void setRespPlayerId(long respPlayerId) {
		this.respPlayerId = respPlayerId;
	}

	public String getRespPlayerName() {
		return respPlayerName;
	}

	public void setRespPlayerName(String respPlayerName) {
		this.respPlayerName = respPlayerName;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public Map<Long, Integer[]> getAsset() {
		return asset;
	}

	public void setAsset(Map<Long, Integer[]> asset) {
		this.asset = asset;
	}

	public Map<Long, List<Integer>> getGoods() {
		return goods;
	}

	public void setGoods(Map<Long, List<Integer>> goods) {
		this.goods = goods;
	}

	public int getReqPlayerLevel() {
		return reqPlayerLevel;
	}

	public void setReqPlayerLevel(int reqPlayerLevel) {
		this.reqPlayerLevel = reqPlayerLevel;
	}

	public int getRespPlayerLevel() {
		return respPlayerLevel;
	}

	public void setRespPlayerLevel(int respPlayerLevel) {
		this.respPlayerLevel = respPlayerLevel;
	}

	/**
	 * 关闭交易
	 * 
	 * @param playerId
	 *            任何一个交易者的Id
	 * @param event
	 */
	public synchronized void canelTrade(String message, long playerId) {
		closeTrade(message, ConstantsRadio.tradeCancel, playerId, getPlayerId(playerId));
	}

	/**
	 * 关闭交易
	 * 
	 * @param playerId
	 * @param event
	 */
	public synchronized void closeTrade(String message, String event, Long... playerId) {

		Context.getDataServer().getPlayerTradeMap().remove(reqPlayerId);
		Context.getDataServer().getPlayerTradeMap().remove(respPlayerId);
		Context.getPlayerServer().getByPlayerId(reqPlayerId).stateRemove(Constants.playerState128_trade);
		Context.getPlayerServer().getByPlayerId(respPlayerId).stateRemove(Constants.playerState128_trade);

		// 解锁所有物品
		for (Long p : goods.keySet()) {
			Collection<Integer> goodsStorages = goods.get(p);
			PlayerBoxs playerBoxs = Context.getPlayerServer().getByPlayerId(p).getPlayerBoxs();
			for (Integer storage : goodsStorages) {
				playerBoxs.unlockGoods(storage);
			}
		}
		EventNotice notice = new EventNotice(ConstantsRadio.trade, event);
		notice.put("message", message);
		notice.broadcast(playerId);
	}

	/**
	 * 返回对方Id
	 * 
	 * @param playerId
	 */
	public long getPlayerId(long playerId) {
		return reqPlayerId == playerId ? respPlayerId : reqPlayerId;
	}

	/**
	 * 判断指定用户是否锁定
	 * 
	 * @param playerId
	 * @return
	 */
	public boolean isLock(long playerId) {
		return (status & (reqPlayerId == playerId ? 4 : 8)) > 0;
	}

	/**
	 * 判断是否有人锁定过
	 * 
	 * @return
	 */
	public boolean hasLock() {
		return (status & 4) > 0 || (status & 8) > 0;
	}
	
	public boolean allLock(){
		return (status & 4) > 0 && (status & 8) > 0;
	}

	/**
	 * 锁定
	 * 
	 * @param playerId
	 */
	public synchronized void lock(long playerId) {
		status |= (reqPlayerId == playerId ? 4 : 8);
	}

	/**
	 * 确定交易
	 * 
	 * @param playerId
	 */
	public synchronized void comfirm(long playerId) {
		status |= (reqPlayerId == playerId ? 1 : 2);
	}

	public int getReqVocationId() {
		return reqVocationId;
	}

	public void setReqVocationId(int reqVocationId) {
		this.reqVocationId = reqVocationId;
	}

	public int getRespVocationId() {
		return respVocationId;
	}

	public void setRespVocationId(int respVocationId) {
		this.respVocationId = respVocationId;
	}

}
